var app = getApp();
var maxPageIndex;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    orderByHand: '',
    buthiddenCancel: false,
    pageIndex: 1,
    queryInfo: '',
    result: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    var that = this;
    if (options.orderByHand) {
      that.setData({ orderByHand: options.orderByHand });
    }
  },

  onShow: function() {
    var that = this;
    wx.pageScrollTo({ scrollTop: 0, duration: 0 });
    that.setData({ pageIndex: 1, queryInfo: '' });
    that.onSupplierInfo();
  },
  searchSupplier: function(e) {
    var that = this;
    that.setData({ queryInfo: e.detail.value });
  },
  bindOnSupplierInfo() {
    this.setData({ pageIndex: 1 });
    this.onSupplierInfo();
  },
  //客户管理列表信息
  onSupplierInfo: function() {
    var that = this;
    // console.log(that.data.queryInfo)
    var arr = new Array();
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getSupplierByQueryInfo',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        queryInfo: that.data.queryInfo, //查询信息(输入姓名或者手机号码)
        packerId: app.globalData.userId,
        page: that.data.pageIndex, //这里初始值为1
        size: 8
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res.data);
        if (res.data.code == 100) {
          maxPageIndex = res.data.total / 8;
          arr = that.jiequ(res.data.result); //判断是不是回收员
          if (that.data.pageIndex != 1) {
            var array = that.data.result;
            array = array.concat(arr);
            that.setData({ result: array });
          } else {
            that.setData({ result: arr });
          }
          that.setData({ ifShownoData: true });
        } else if (res.data.code == 400) {
          that.setData({ ifShownoData: false, result: [] });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          });
        }
      },
      fail: function(error) {}
    });
  },
  jiequ(arr) {
    arr.forEach(function(value, index, array) {
      let item = value.userId.slice(0, 3);
      if (item === 'sp_') {
        arr[index].userType = 0;
      } else {
        arr[index].userType = 1;
      }
    });
    return arr;
  },
  //删除客户事件
  onDelSupplier: function(e) {
    var that = this;
    var userId = e.currentTarget.dataset.userid;
    console.log(userId);
    wx.showModal({
      title: '提示',
      content: '确认删除该供应商信息',
      cancelText: '再等等',
      confirmColor: '#50BEC5',
      success: function(res) {
        if (res.confirm) {
          console.log(app.preUrl + 'deleteSupplierUser');
          that.setData({ buthiddenCancel: true });
          wx.request({
            method: 'POST',
            url: app.preUrl + 'deleteSupplierUser',
            dataType: 'json',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              userId: userId,
              packerId: app.globalData.userId
            },
            success: function(res) {
              if (res.data.code == 100) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none',
                  duration: 1000
                });
                that.setData({
                  buthiddenCancel: false,
                  pageIndex: 1,
                  queryInfo: ''
                });
                that.onSupplierInfo();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.message,
                  confirmColor: '#56BDE0',
                  showCancel: false
                });
              }
            },
            fail: function(error) {
              that.setData({ buthiddenCancel: false });
            }
          });
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    });
  },
  //选择客户事件
  goOrderByHand: function(e) {
    console.log(e.currentTarget.dataset.myid);
    wx.setStorage({
      key: 'supplierPackerId',
      data: e.currentTarget.dataset.myid
    });
    var that = this;
    if (that.data.orderByHand == 1) {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        nameId: e.currentTarget.dataset.userid,
        nameText: e.currentTarget.dataset.name,
        idNumber: e.currentTarget.dataset.cardnumber,
        nameStatus: true
      });
      wx.navigateBack({
        delta: 1
      });
    }
  },
  //修改客户信息
  toModifySupplier: function(e) {
    var phone = e.currentTarget.dataset.phone;
    wx.navigateTo({
      url: 'supplier_modify?phone=' + phone
    });
  },
  //添加客户
  toAddSupplier: function() {
    wx.navigateTo({
      url: 'supplier_add'
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      result: []
    });
    wx.stopPullDownRefresh();
    that.onSupplierInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (maxPageIndex <= that.data.pageIndex) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    });

    // 上拉获取更多数据
    that.onSupplierInfo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
